//
//  RegisterViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/23.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *unameLbale;
@property (weak, nonatomic) IBOutlet UITextField *secreatLable;
@property (weak, nonatomic) IBOutlet UITextField *sureLable;
@property (weak, nonatomic) IBOutlet UITextField *emaiLable;
@property (weak, nonatomic) IBOutlet UITextField *emilLable;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)registAction:(UIButton *)sender {

    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    if ([self setUserToprepareRegisterWithUser:user]) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    // 注册成功
                    NSLog(@"注册成功");
                    [self showAlertViewWithMessage:@"注册成功"];
                } else {
                    // 失败的原因可能有多种，常见的是用户名已经存在。
                    NSLog(@"%ld",error.code);
                    [self showAlertViewWithMessage:@"注册失败"];
                }
            }];
        });

    }

}

-(BOOL)setUserToprepareRegisterWithUser:(AVUser*)user{

    user.username = self.unameLbale.text;// 设置用户名
    if ([self.secreatLable.text isEqualToString:self.sureLable.text]) {
        user.password = self.sureLable.text;
    }
    user.email = self.emaiLable.text;// 设置邮箱

    return YES;
}


-(void)showAlertViewWithMessage:(NSString*)message{

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {


    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{

    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
